package com.aijk.ehm.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by banxia on 2016/4/28.
 */
public class SerialUtil {

    private static final int MIN = 999999999;

    private static final String PATTERN_1 = "yyMMdd";

    private static final String PATTERN_2 = "yyyyMMdd";

    private static final SimpleDateFormat
                SDF1 = new SimpleDateFormat(PATTERN_1);

    private static final SimpleDateFormat
                SDF2 = new SimpleDateFormat(PATTERN_2);

    private static long preValue = 0L;

    public static String serial_10() {
        int uuid_hash = UUID.randomUUID().toString().hashCode();
        uuid_hash = (uuid_hash > 0) ? uuid_hash : (- uuid_hash);
//        String serial = "" + uuid_hash;
        String serial = String.format("%010d",uuid_hash);
        return serial;
//        return (uuid_hash > MIN) ? serial : ("0" + serial);
    }

    public static String serial_10(String prefix, String suffix) {
        String serial = serial_10();
        serial = (prefix == null) ? serial : (prefix + serial);
        serial = (suffix == null) ? serial : (serial + suffix);
        return serial;
    }

    public static String serial_16() {
        return SDF1.format(new Date()) + serial_10();
    }

    public static String serial_16(String prefix, String suffix) {
        String serial = serial_16();
        serial = (prefix == null) ? serial : (prefix + serial);
        serial = (suffix == null) ? serial : (serial + suffix);
        return serial;
    }

    public static String serial_18() {
        return SDF2.format(new Date()) + serial_10();
    }

    public static String serial_18(String prefix, String suffix) {
        String serial = serial_18();
        serial = (prefix == null) ? serial : (prefix + serial);
        serial = (suffix == null) ? serial : (serial + suffix);
        return serial;
    }

    public static String invite_code() {
        long nowValue = System.currentTimeMillis();
        while(preValue == nowValue)
        {
            nowValue = System.currentTimeMillis();
        }
        preValue = nowValue;
        String strValue = Long.toString(nowValue, 36);
        return strValue;
    }

    public static String[] chars = new String[] {  "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z" };


    public static String generateShortUuid() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 6; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x24]);
        }
        return shortBuffer.toString();
    }

    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        for(int i=0;i<1000000 ;i++) {
            String code= invite_code();
            System.out.println(code);
            if(map.containsKey(code)){
                System.out.println("出现重复");
                break;
            }else {
                map.put(code, code);
            }
        }
    }

}
